package com.v1.video.view;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.graphics.Color;
import android.text.Selection;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.StyleSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

public class TextViewExtend extends TextView {
	public interface OnSpanClickListener{
		public void click(String value,View widget);
	}
	private Context mContext;
	private int mHighLightTextColor = Color.rgb(0x00,0x99,0xCC);
	private int mDefaultBackColor = Color.TRANSPARENT;
	private int mPressedBackColor = Color.YELLOW;
	private Pattern pattern = null;
	private String mRegularStr = "(#(.+?)#)";
	private OnSpanClickListener mSpanClickListener;
	
	/**
	 * span颜色
	 * @param mHighLightTextColor
	 */
	public void setHighLightTextColor(int mHighLightTextColor) {
		this.mHighLightTextColor = mHighLightTextColor;
	}

	/**
	 * span 的匹配规则
	 * @param mRegularStr
	 */
	public void setRegularStr(String mRegularStr) {
		this.mRegularStr = mRegularStr;
	}
	
	/**
	 * span 点击后的回调方法
	 * @param listener
	 */
	public void setOnSpanClickListener(OnSpanClickListener listener){
		mSpanClickListener = listener;
	}


	public TextViewExtend(Context context) {
		super(context);
		init(context,null);
	}

	public TextViewExtend(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context,attrs);
	}
	
	public TextViewExtend(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context,attrs);
	}
	
	private void init(Context context, AttributeSet attrs){
		mContext = context;		
		if (!TextUtils.isEmpty(mRegularStr))
			pattern = Pattern.compile(this.mRegularStr);
	}

    @Override
	public boolean hasFocusable() {
		return false;
	}
	
	public void setSpanText(String value,String boldString){
		if(TextUtils.isEmpty(value))
			return;
		SpannableString sp = null;
		Matcher matcher = pattern.matcher(value);
		sp = new SpannableString(value);
		while(matcher.find()){
			sp.setSpan(new Clickable(matcher.group(),false), matcher.start(), matcher.end(), SpannableString.SPAN_EXCLUSIVE_INCLUSIVE);
		}
		
		if(!TextUtils.isEmpty(boldString)){
			int start = value.indexOf(boldString);
			if (start >= 0){
				sp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), start, start + boldString.length(),
						Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			}
		}
		
		this.setText(sp);
		this.setMovementMethod(LinkMovementMethod.getInstance());
	}
	
	/**
	 * 继承URLSpan也可以
	 * @author shenjin
	 *
	 */
	private class Clickable extends ClickableSpan{
		
		String spanVal;
		boolean isUrl = false;
		
		public Clickable(String value,boolean isUrl){
			this.spanVal = value;
			this.isUrl = isUrl;
		}

		@Override
		public void updateDrawState(TextPaint ds) {
			super.updateDrawState(ds);
			ds.setUnderlineText(false);
			ds.setColor(mHighLightTextColor);
		}

		@Override
		public void onClick(View widget) {
			Spannable spannable = (Spannable) ((TextView) widget).getText();
			Selection.removeSelection(spannable);
			if(mSpanClickListener != null){
				mSpanClickListener.click(spanVal,widget);
			}
		}
		
	}
}
